/**
 * h5游戏sdk cp端引入
 */
(function($,windows){
    var debug = function(msg){
        console.log(msg);
    }

    var GameSdk = function(){};//sdk
    GameSdk.SDK_VERSION = '20180309';//版本号
    GameSdk.SDK_ENV = 'prod';//正式测试环境切换

    GameSdk.enentCalls = {};
    GameSdk.API_CONFIG = function(){
            if(GameSdk.SDK_ENV=='prod'){//正式测试切换
                return {
                    CREATE_ROLE_URL : 'http://h5.local/index.php?a=createrole',//创角接口
                    GET_USERINFO_URL : 'http://h5.local/index.php?a=getuserinfo',//获取用户接口
                    GET_PLATFORM_CONFIG : 'http://h5.local/index.php?a=getconfigforcp'//获取配置信息
                }
            }else{
                return {
                    CREATE_ROLE_URL : 'http://h5.local/index.php?a=createrole',//创角接口
                    GET_USERINFO_URL : 'http://h5.local/index.php?a=getuserinfo',//获取用户接口
                    GET_PLATFORM_CONFIG : 'http://h5.local/index.php?a=getconfigforcp'//获取配置信息
                }
            }

        }
    
    /**
     * http get 封装
     * @param {*请求url} url 
     * @param {*参数} params 
     */
    GameSdk.httpGet = function(url,params){
        $.ajax({
            type:'get',
            data:params,
            url:url,
            dataType:"jsonp",
            jsonp:'callback',
            success:function(data){
                if(data.status!=0){
                    alert(data.msg);
                }
                debug(data.msg);
            }
        });
    }
    /**
     * http get callback
     * @param {*url} url 
     * @param {*参数} params 
     * @param {*函数回调} callback 
     */
    GameSdk.httpGetCallback = function(url,params,callback){
        console.log(url);
        console.log(params);
        console.log(callback);
        $.ajax({
            type:'get',
            data:params,
            url:url,
            dataType:"jsonp",
            jsonp:'callback',
            success:function(data){
                console.log('http get callback');
                if(data.status!=0){
                    alert(data.msg);
                }
                debug(data.msg);
                callback(data.data);
            }
        });
    }
    /**
     * 获取平台对应的配置信息
     * @param {*参数} params 
     * @param {*回调} callback 
     */
    GameSdk.config = function(params,callback){
        console.log("config...");
      
        GameSdk.httpGetCallback(GameSdk.API_CONFIG().GET_PLATFORM_CONFIG,params,callback);
    }

    /**
     * 初始化
     * @param {*} params   初始化参数 
     * @param {*} callback 回调函数
     */
    GameSdk.init = function(params,callback){
        if(!params.cp_id || !params.game_id){
            alert('初始化参数错误,参数缺失');
            return false;
        }
        GameSdk.config(params,callback);
        debug('cp初始化成功');
    }

    /**
     * 退出
     */
    GameSdk.logout = function(){
        var data = {
            'event':'bh_logout'
        };
        window.parent.postMessage(data,'*');
    }
    
    /**
     * 调用支付
     */
    GameSdk.pay = function(params){
        var data = {
            'event':'bh_pay',
            'payParams':params
        };
        window.parent.postMessage(data,'*');
    }
    /**
	 * 上报创建角色事件
	 *
     * @param param
	 * 我方平台用户id platform_uid
	 * 游戏id game_id
	 * 服务id号 server_id
	 * 服务器名称 server_name
	 * 角色id（没有为空）role_id
	 * 角色名 role_name
	 * 角色等级 role_level
	 * 创角时间(精确到秒unix格式) create_time
     */
    GameSdk.createRole = function(params){
        GameSdk.httpGet(GameSdk.CREATE_ROLE_URL,param);
    }

    /**
	 * 获取用户信息
	 * @param  userInfo
	 * cp_id          cpid
	 * game_id        游戏id
	 * access_token   access_token
	 * timestamp      请求时间(unix格式)
	 * sign           签名
	 * @param callback 回调
	 */
    GameSdk.getUserInfo = function(params,callback){
        GameSdk.httpGetCallback(GameSdk.GET_USERINFO_URL,param,callback);
    }


     /**
	 * 游戏内手动点击分享(邀请)按钮
     * @param callback 分享回调
     */
	GameSdk.share = function(callback) {
        var data = {
            'event' : 'bh_share'
        };

        //注册消息回调
        GameSdk.eventCalls['share_success_back'] = callback;

        window.parent.postMessage(data, '*');
	};

    /**
	 * 显示关注二维码
     */
	GameSdk.showFollow = function(){
        var data = {
            'event' : 'bh_weixinfollow'
        };
        window.parent.postMessage(data, '*');
	};


    /**
     * 接受平台传递过来的消息
     * @param e
     */
    var receive = function(e){

        debug('接受平台传递过来的消息:');
        console.log(e.data);
        if( e.data.event &&  GameSdk.eventCalls[e.data.event] ){
            GameSdk.eventCalls[e.data.event]();
        }
    }

    //监听平台消息
    window.addEventListener('message', receive, false);

	window.GameSdk = GameSdk;
	//-------------util--------------
	function GameUtil() {
	};

	GameUtil.isMobile = function() {
	    var mobileAgent = new Array("iphone", "ipod", "ipad", "android", "mobile", "blackberry", "webos", "incognito", "webmate", "nokia", "ucweb", "skyfire");
	    var ua = navigator.userAgent.toLowerCase();
	    for (var i = 0; i < mobileAgent.length; i++) {
	        if (ua.indexOf(mobileAgent[i]) != -1) {
	            return true
	        }
	    };
	    return false
	};

	GameUtil.isIOS = function() {
	    return /iPhone|iPod|iPad|Mac/ig.test(navigator.userAgent)
	};

	GameUtil.isAndroid = function() {
	    return /android|linux/i.test(navigator.userAgent)
	};

	GameUtil.isWindowsWechat = function() {
	    return /WindowsWechat/ig.test(navigator.userAgent)
	};

	GameUtil.isWeixin = function() {
	    return /micromessenger/ig.test(navigator.userAgent)
	};

	GameUtil.isClient = function() {
	    return /qiwanhezi/ig.test(navigator.userAgent)
	};

    GameUtil.getQueryString = function(name) {
        var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
        var r = window.location.search.substr(1).match(reg);
        if (r != null) {
            return decodeURIComponent(r[2]);
        }
        return null;
    }
})(jQuery, window);